package comp512.classes;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.Timer;

@SuppressWarnings("serial")
public class TimerLabel extends JLabel implements ActionListener {
	private Timer t;
	private int secondsLeft;
	public TimerLabel() {
		super("Timer: 00:00");
		t = new Timer(1000, this);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(secondsLeft == 0)
		{
			t.stop();
		}
		setText("Timer: 00:" + pad());
		this.setHorizontalAlignment(LEFT);
		secondsLeft--;
	}

	public void startTimer(int seconds){
		this.secondsLeft = seconds;
		t.start();
	}
	
	private String pad(){
		if(secondsLeft < 10)
			return "0" + secondsLeft;
		else return String.valueOf(secondsLeft);
	}
}
